package com.fengba.nanzhi.myfengba;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.login.Login;
import com.fengba.nanzhi.tabbar01.FXDetailedAdapter;
import com.fengba.nanzhi.tabbar01.FX_FD_Detailed;
import com.fengba.nanzhi.utils.FX_SignInUtil;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class OrderDetailed extends Activity {

    private String id;

    JSONObject jsonObject1;
    //姓名
    private TextView FODtv02;
    //地址
    private TextView FODtv04;
    //电话
    private TextView FODtv03;
    //商品名称
    private TextView FODtv05;
    //商品价格
    private TextView FODtv06;
    //数量
    private TextView FODtv12;
    //运费
    private TextView FODtv13;
    //合计金额
    private TextView FODtv14;
    //应付总额
    private TextView FODtv15;
    //疯豆数量
    private TextView FODtv22;
    //订单编号
    private TextView FODtv18;
    //支付方式
    private TextView FODtv19;
    //创建时间
    private TextView FODtv21;
    //疯豆logo
    private ImageView FODiv02;
    //商品图片
    private ImageView FODiv01;
    //订单状态
    private TextView FODtv01;
    //确认支付
    private Button FODbtn01;
    //支付框框
    private RelativeLayout FODrl06;



    public OrderDetailed() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_orderdetailed);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        init();

        /**
         * 返回
         */
        ImageButton Nbtn01 = (ImageButton)findViewById(R.id.Nbtn01);
        Nbtn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }//都在这里打
    public void init()
    {
        FODtv02 = (TextView) findViewById(R.id.FODtv02);
        FODtv04 = (TextView) findViewById(R.id.FODtv04);
        FODtv03 = (TextView) findViewById(R.id.FODtv03);
        FODtv05 = (TextView) findViewById(R.id.FODtv05);
        FODtv06 = (TextView) findViewById(R.id.FODtv06);
        FODtv12 = (TextView) findViewById(R.id.FODtv12);
        FODtv13 = (TextView) findViewById(R.id.FODtv13);
        FODtv14 = (TextView) findViewById(R.id.FODtv14);
        FODtv15 = (TextView) findViewById(R.id.FODtv15);
        FODtv22 = (TextView) findViewById(R.id.FODtv22);
        FODiv02 = (ImageView) findViewById(R.id.FODiv02);
        FODtv18 = (TextView) findViewById(R.id.FODtv18);
        FODtv19 = (TextView) findViewById(R.id.FODtv19);
        FODtv21 = (TextView) findViewById(R.id.FODtv21);
        FODiv01 = (ImageView) findViewById(R.id.FODiv01);
        FODtv01 = (TextView) findViewById(R.id.FODtv01);
        FODbtn01 = (Button) findViewById(R.id.FODbtn01);
        FODrl06 = (RelativeLayout) findViewById(R.id.FODrl06);
            OKManager.getInstance().type2018(id, new OKManager.func4() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        if (jsonObject.get("code").equals(0)) {
                            jsonObject1 = (JSONObject) jsonObject.get("data");
                            FODtv02.setText(jsonObject1.get("address_name").toString());
                            FODtv04.setText(jsonObject1.get("address_address").toString());
                            FODtv03.setText(jsonObject1.get("address_account").toString());
                            FODtv05.setText(jsonObject1.get("order_name").toString());
                            FODtv06.setText("￥"+jsonObject1.get("order_money").toString());
                            FODtv12.setText("X"+jsonObject1.get("buy_number").toString());
                            FODtv13.setText("￥"+jsonObject1.get("freight").toString());
                            double d2 = Double.parseDouble(jsonObject1.get("order_money").toString());
                            double d1 = Double.parseDouble(jsonObject1.get("freight").toString());
                            int i = Integer.parseInt(String.valueOf(jsonObject1.get("buy_number").toString()));
                            FODtv14.setText("￥"+(d2*i+d1)+"");
                            if (!jsonObject1.get("credit").toString().equals("0")) {
                                FODtv22.setText(jsonObject1.get("credit").toString());
                                FODtv15.setText(String.valueOf(FODtv14.getText())+"+");
                            }else {
                                FODtv15.setText(String.valueOf(FODtv14.getText()));
                                FODiv02.getLayoutParams().width = 0;
                            }
                            FODtv18.setText(jsonObject1.get("order_bh").toString());
                            FODtv19.setText("支付宝");
                            FODtv21.setText(jsonObject1.get("createtime").toString());
                            switch (jsonObject1.get("status").toString())
                                    //订单状态0待支付,1已付款,2 已取消,3未开奖,4未中奖(预付款退回中),5未中奖(预付款已退回)',
                            {
                                case "0":FODtv01.setText("待支付");
                                    FODrl06.setVisibility(View.VISIBLE);break;
                                case "1":
                                    if (jsonObject1.get("delivery").toString().equals("0")) {
                                        FODtv01.setText("代发货");
                                    }else
                                    {
                                        FODtv01.setText("已发货");
                                    }
                                ;break;
                                case "2":FODtv01.setText("已取消");break;
                                case "3":FODtv01.setText("未开奖");break;
                                case "4":FODtv01.setText("未中奖(预付款退回中)");break;
                                case "5":FODtv01.setText("未中奖(预付款已退回)");break;
                            }
                            Glide.with(OrderDetailed.this).load(jsonObject1.get("images").toString().split(",")[0]).into(FODiv01);
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Calendar nowTime = Calendar.getInstance();
                            nowTime.add(Calendar.MINUTE, 15);
                            String oidtime = sdf.format(nowTime.getTime());
                            Date oldData = sdf.parse(oidtime);
                            if (oldData.after(new Date()))
                            {
                                FODtv01.setText("已取消");
                                FODrl06.setVisibility(View.INVISIBLE);
                            }
                        } else {
                            Toast.makeText(OrderDetailed.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //refreshLayout.finishRefresh();
                }

                @Override
                public void onFailure(String string) {
                    Toast.makeText(OrderDetailed.this, "网络错误", Toast.LENGTH_SHORT).show();
                    //refreshLayout.finishRefresh();
                }
            });
    }

}
